/*
 * @Author: your name
 * @Date: 2021-11-30 09:28:58
 * @LastEditTime: 2021-12-16 14:20:02
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \express-app\routes\users.js
 */
const express = require('express');
const router = express.Router();
const UsersModel = require('../mongoose/users.js');

/* GET users listing. */
router.get('/', async (req, res, next) => {
  let { pageSize, page } = req.query;
  let result = await UsersModel.find({})
    .limit(pageSize)
    .skip((page - 1) * pageSize);
  const total = await UsersModel.find().count();
  res.send({
    msg: 'ok',
    result,
    total,
  });
});
router.post('/add', async (req, res, next) => {
  let user = new UsersModel({
    name: '王五',
    age: 22,
  });
  await user.save();
  res.send({
    msg: 'ok',
  });
});
router.post('/edit', async (req, res, next) => {
  res.send('respond with a edit');
});
router.post('/delete', async (req, res, next) => {
  res.send('respond with a delete');
});

module.exports = router;
